﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MyVideoViewerLibrary;
using System.Windows.Threading;
using System.Net;

namespace TrailersDownloader
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Downloader downloader;
        private DateTime start;
        public MainWindow()
        {
            InitializeComponent();
            downloader = new Downloader();
            downloader.Complete += new DownloaderEvent(downloader_Complete);
            downloader.DownloadProgressChanged += new DownloadProgressChangedEvent(downloader_DownloadProgressChanged);
            downloader.StatusChanged += new StatusChangedEvent(downloader_StatusChanged);
            downloader.DownloadStarted += new DownloaderEvent(downloader_DownloadStarted);

            cancelButton.Click += new RoutedEventHandler(cancelButton_Click);

            Loaded += new RoutedEventHandler(Window1_Loaded);
        }
        void downloader_DownloadStarted()
        {
            start = DateTime.Now;
        }

        void downloader_StatusChanged(string status)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                statusTextBlock.Text = status;


            });
        }

        void downloader_DownloadProgressChanged(DownloadProgressChangedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                currentProgressBar.Value = e.ProgressPercentage;


                double ByteProcessTime = 0;
                double EstimatedTime = 0;

                TimeSpan timeSpan = DateTime.Now - start;
                ByteProcessTime = e.BytesReceived / timeSpan.TotalSeconds;
                EstimatedTime = (e.TotalBytesToReceive - e.BytesReceived) / ByteProcessTime;
                timeSpan = TimeSpan.FromSeconds(EstimatedTime);

                timeLeft.Text = string.Format("{0:00}:{1:00}:{2:00}", timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds);
            });
        }

        void downloader_Complete()
        {
            this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)delegate()
            {
                Close();
            });
        }


        void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            downloader.Cancel();
        }


        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            statusTextBlock.Text = "Loading settings";
            if (Settings.Default == null || string.IsNullOrEmpty(Settings.Default.TrailersDownloadFolder))
            {
                MessageBox.Show("The download folder has not been set, please run the configuration tool and select a folder to download to.");
                Application.Current.Shutdown();
                return;
            }
            statusTextBlock.Text = "Starting";
            downloader.Start();
        }
    }
}
